package m.jclecai.com.fragment.message;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.NoticeBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.AutoSwipeRefreshLayout;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Dinosa on 2017/9/17.
 * 公告的activity
 */
public class NoticeFragment extends BaseFragment {


    @BindView(R.id.srl_layout)
    AutoSwipeRefreshLayout mSrlLayout;
    @BindView(R.id.rv_notice)
    RecyclerView mRvNotice;

    public static final int NOTICE=1;
    public static final int SYSTEM_MESSAGE =0;
    public static final int USER_MESSAGE=2;

    private Subscription mSubscribe;
    private List<NoticeBean.RecordsBean> mRecords;
    private NoticeAdapter mNoticeAdapter;

    int itemViewRes;
    private int mType;
    private String mStrType;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_notice;
    }


    @Override
    public void initData() {

        mRvNotice.setLayoutManager(new LinearLayoutManager(getActivity()));
        mNoticeAdapter = new NoticeAdapter(null);
        mRvNotice.setAdapter(mNoticeAdapter);
        mSrlLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestNoticeData();
            }
        });
        requestNoticeData();
    }

    private void requestNoticeData() {

        //这里我们要做的一个操作就是刷新界面；
        mSubscribe = Observable.just(true).map(new Func1<Boolean, NoticeBean>() {
            @Override
            public NoticeBean call(Boolean aBoolean) {

                return doRequest();
            }
        })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<NoticeBean>() {

                    @Override
                    public void onStart() {
                        super.onStart();
                        mSrlLayout.setRefreshing(true);
                    }

                    @Override
                    public void onCompleted() {
                        mSrlLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        DisPlay("网络连接异常！！！");
                        mSrlLayout.setRefreshing(false);
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(NoticeBean noticeBean) {
                        if (noticeBean != null) {
                            //这里我们要做的一个操作刷新界面；
                            mRecords = noticeBean.getRecords();
                            mNoticeAdapter.refresh(mRecords);
                        }
                    }
                });

    }

    private NoticeBean doRequest() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("user",PreferenceUtil.getUserName(getActivity()));
        hashMap.put("password", PreferenceUtil.getPassword(getActivity()));
        hashMap.put("type",mStrType);
        String postAsString = null;
        NoticeBean noticeBean= null;
        try {
            postAsString = HttpManager.postAsString(Constant_Url.NOTICE_LIST, hashMap);
            noticeBean = FastOk.get(postAsString, NoticeBean.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return noticeBean;
    }

    @Override
    public void onDestroy() {
        if (mSubscribe != null) {
            mSubscribe.unsubscribe();
        }
        super.onDestroy();
    }
    public class NoticeAdapter extends BaseRecyclerViewAdapter<NoticeBean.RecordsBean>{

        public NoticeAdapter(List<NoticeBean.RecordsBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return itemViewRes;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final NoticeBean.RecordsBean bean, int position) {
            if(mType==NOTICE){
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //这里我们要做的一个操作就是进入相应的网页里面；
                        //进入中；
                        String url="http://m.jclecai.com/news/show.html?id="+bean.getId();
                        getActivity().startActivity(WebHelperActivity.getInstance(getActivity(),url,false,"公告"));
                    }
                });

            }
            holder.getTextView(R.id.tv_type).setText(bean.getClassname());
            holder.getTextView(R.id.tv_date).setText(bean.getAddtime());
            holder.getTextView(R.id.tv_notice).setText(bean.getNotice());
        }
    }

    public void setType(int type){
        mType = type;
        itemViewRes=R.layout.item_message;

       switch (mType) {
            case NOTICE:
                mStrType="官方公告";
                break;
            case SYSTEM_MESSAGE:
                mStrType="系统消息";
                break;
            case USER_MESSAGE:
                mStrType="个人消息";
                break;
        }
    }


}
